package DMSXL._2024.TX;

public class candy {
    public static void main(String[] args){
        int[] ratings = {1,0,2};
        System.out.println(new candy().candy(ratings));
    }
    public int candy(int[] ratings){
        int candyAaary[] = new int[ratings.length];
        candyAaary[0] = 1;
        //从左往右遍历数据，如果右边大于左边，则右边比左边+1
        for (int i = 1; i < ratings.length; i++) {
            candyAaary[i] = (ratings[i] > ratings[i - 1])?candyAaary[i - 1] + 1: 1;
        }
        //从右往左遍历，确保合规
        for (int i = ratings.length - 2; i >= 0; i--) {
            if(ratings[i] > ratings[i + 1]){
                candyAaary[i] = Math.max(candyAaary[i + 1] + 1,candyAaary[i]);
            }
        }

        //计算最终结果
        int result = 0;
        for (int i = 0; i < candyAaary.length; i++) {
            result += candyAaary[i];
        }

        return result;
    }
}
